<?php

/**
 * ECSHOP 超值礼包列表
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: activity.php 16056 2009-05-21 05:44:14Z liubo $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
include_once(ROOT_PATH . 'includes/lib_transaction.php');

/* 载入语言文件 */
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/shopping_flow.php');
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/user.php');
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/admin/package.php');

if(empty($_REQUEST['act'])){
	$_REQUEST['act'] = 'show';
}

/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'show')
{
	$act_id		 = intval($_REQUEST['id'])>0? intval($_REQUEST['id']) : 0; //组合ID
	$package_id  = intval($_REQUEST['id2'])>0? intval($_REQUEST['id2']) : 0; //稿件套装ID

	assign_template();
	assign_dynamic('package');
	$position = assign_ur_here(0,		$_LANG['shopping_package']);
	$smarty->assign('page_title',       $position['title']);    // 页面标题
	$smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
	$smarty->assign('categories',       get_categories_tree());  // 分类树
	//$smarty->assign('categories2',      get_categories_tree2()); //所属行业分类
	$smarty->assign('cfg',          $_CFG);

	$list = get_package_list();
	if($act_id ==0 ){
		$act_id = $list[0]['act_id'];
	}

	if($act_id >0){
		$package	= get_package_info( $act_id );//商品组合数据
		$act_type2 = intval($package['act_type2']); //活动类型ID
		if($act_type2 > 0){
			$package['bb_name'] = get_bb_name($act_type2);//活动类型名称
		}
		$smarty->assign('package', $package);

		$goods2_list = array();
		if($act_type2>0){
			$goods_aa_list = get_goods_aa_list($act_type2);  //稿件套装列表
			if($package_id == 0){
				$package_id    = $goods_aa_list[0]['act_id'];//稿件套餐ID  默认第一个
			}
			$goods2_list   = get_goods_aa_goods($package_id);//套装稿件列表
		}

		$package_goods_list = get_package_goods_list( $act_id ); //组合的商品数据列表

		$data_list  = array();
		foreach($package_goods_list as $row){
			foreach($goods2_list as $goods2){
				if($row['cat_id'] == $goods2['cat_id']){
					$row['goods_thumb'] = $goods2['goods_thumb'];
					$row['goods2_id']	= $goods2['goods_id'];
				}
			}
			$goods2_price = get_final_price($row['goods2_id'], 1, false );//稿件最终价
			$row['total_price'] = "<font class='shop'>".price_format(intval($row['goods_price'])+$goods2_price)."</font>";
			$row['total_price'] .=' [商品价格'.price_format($row['goods_price']).'+ 模板价格'.price_format($goods2_price).']';
			$data_list[] = $row;
		}
	
		$smarty->assign('package_goods_list',  $data_list);//商品列表

		$smarty->assign('goods2_list',	$goods_aa_list);//稿件套装列表

		
		$k = 0 ;
		$cur_num = 6;
		foreach($goods_aa_list as $dd){
			$k +=1;
			if($dd['act_id'] == $package_id){
				$cur_num = $k;
			}
		}
		$smarty->assign('cur_num',$cur_num);   //当前页数

	}


	$smarty->assign('package_id',       $package_id);//当前稿件套装ID
	$smarty->assign('package_list',     $list);//组合列表
	$smarty->assign('helps',            get_shop_help());       // 网店帮助
	$smarty->assign('lang',             $_LANG);
	$smarty->assign('menu_index',             8);

	$smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-typepackage.xml" : 'feed.php?type=package'); // RSS URL
	$smarty->display('package.dwt');
}

/*------------------------------------------------------ */
//-- 改变套餐数量时，重新计算商品价格
/*------------------------------------------------------ */

if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'taocan_price')
{
    include('includes/cls_json.php');
    $json   = new JSON;
    $res    = array('err_msg' => '', 'result' => '', 'qty' => 1);
	$gmid      = isset($_REQUEST['gmid']) ? explode('|', $_REQUEST['gmid']) : array();
	$package_id = empty($_REQUEST['package_id'])?0:intval($_REQUEST['package_id']);
	
    if (count($gmid) <= 0)
    {
        $res['err_msg'] = $_LANG['err_change_attr'];
        $res['err_no']  = 1;
    }
    else
    {
		$rs = '&nbsp;&nbsp;&nbsp;&nbsp;您已选购商品：<br/>';
		$total = 0;
		foreach($gmid as $key=>$da){
			$gm = explode('#', $da);
			$goods_id = intval($gm[0]);
			$goods2_id = intval($gm[1]);

			if($goods_id >0){
				$shop_price  = get_price($package_id,$goods_id);
				$shop_price2 = get_final_price($goods2_id, 1 , false, $attr_id );
				$total +=($shop_price+$shop_price2);
				$rs .= '&nbsp;&nbsp;&nbsp;&nbsp;'.$_LANG['goods_sn'].get_goods_sn($goods_id).'&nbsp;&nbsp;&nbsp;&nbsp;商品总价：<font class="shop">'.price_format($shop_price+$shop_price2 ).'</font>&nbsp;[商品价格'.price_format($shop_price).'+模板价格'.price_format($shop_price2).']<br/>';
			}
		}

		$total = price_format($total);
		$rs .= '&nbsp;&nbsp;&nbsp;&nbsp;总计：<font style="color:red;">'.$total.'</font><br/>';
		$res['result'] = $rs;
    }

    die($json->encode($res));
}

/*------------------------------------------------------ */
//-- 获取稿件列表
/*------------------------------------------------------ */

if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'goods2_list')
{
    include('includes/cls_json.php');
    $json   = new JSON;
    $res    = array('err_msg' => '', 'result' => '', 'qty' => 1);
	$cat_id = empty($_REQUEST['cat_id'])?0:intval($_REQUEST['cat_id']); //所属商品分类ID
	$cat2_id = empty($_REQUEST['cat2_id'])?0:intval($_REQUEST['cat2_id']); //所属行业ID
	$goods_id     = empty($_REQUEST['goods_id'])?0:intval($_REQUEST['goods_id']); //商品ID
	$goods2_id    = empty($_REQUEST['goods2_id'])?0:intval($_REQUEST['goods2_id']); //稿件ID
	$package_id   = empty($_REQUEST['package_id'])?0:intval($_REQUEST['package_id']); //稿件套装ID
	$page		  = intval($_REQUEST['page']) > 0 ?intval($_REQUEST['page']):1; //分页页码
	
    if (count($cat_id) <= 0)
    {
        $res['err_msg'] = $_LANG['err_change_attr'];
        $res['err_no']  = 1;
    }
    else
    {
		$cat_list = get_categories_tree2();
		$here = '';

		$rs  = '<dl class="check_list">';
		$rs .= '<dt><strong>所属行业: </strong></dt>';
		$rs .= '<dd>';
		$rs .= '<a class="'.(($cat2_id==0)?"here":"").'" href="javascript:;" onclick="load_datas('.$cat_id.','.$goods_id.','.$goods2_id.','.$package_id.',0)">全部</a>';
				
		foreach($cat_list as $cat){
			$rs .= '<a class="'.(($cat2_id==$cat['id'])?"here":"").'" href="javascript:;" onclick="load_datas('.$cat_id.','.$goods_id.','.$goods2_id.','.$package_id.','.$cat['id'].')">'.$cat['name'].'</a>';
			foreach($cat['cat_id'] as $child){
				$rs .= '<a class="'.(($cat2_id==$child['id'])?"here":"").'" href="javascript:;" onclick="load_datas('.$cat_id.','.$goods_id.','.$goods2_id.','.$package_id.','.$child['id'].')">'.$child['name'].'</a>';
			}
		}
		$rs .='</dd></dl><br/>';


		$rs .= '<div class="ul_goods2_list"><ul>';
		$shop_price  = get_price($package_id,$goods_id);//商品价格
		//同分类下的稿件列表
		$page_total = category_get_goods2_count($cat_id,$cat2_id);
		$page_size = 12;
		$page_count = ceil($page_total/$page_size);
		$goodslist2 = category_get_goods2($cat_id , $cat2_id,$goods2_id, $page_size ,$page );
		foreach($goodslist2 as $goods2){
			$total_prices = '';
			$shop_price2 = get_final_price($goods2['goods_id'], 1 , false);//稿件最终价格
			$total +=($shop_price+$shop_price2);
			$total_prices  = "<font class='shop'>".price_format(intval($shop_price)+$shop_price2)."</font>";
			$total_prices .=' [商品价格'.price_format($shop_price).'+ 模板价格'.price_format($shop_price2).']';

			$rs .='<li class="img_li"><a href="javascript:;" title="点击选择稿件" _title="'.$total_prices.'" goods_id="'.$goods_id.'" goods2_id="'.$goods2['goods_id'].'"><img src="'.get_image_path($goods2['goods_id'], $goods2['goods_img'], true).'"/></a><p>'.$goods2['goods_name'].'</p><p>模板价格:'.price_format($shop_price2).'</p></li>';
		}
		
		$rs .='</ul><div id="pager" style="margin-right:10px;" class="pagebar"><span class="f_l f6">总计<b>'.$page_total.'</b>个记录</span>';
		for($k = 1;$k<=$page_count;$k++){
			if($page == $k){
				$rs .='<span class="page_now">'.$k.'</span>';
			}else{
				$rs .='<a href="javascript:;" onclick="load_datas('.$cat_id.','.$goods_id.','.$goods2_id.','.$package_id.','.$cat2_id.','.$k.')">['.$k.']</a>';
			}
		}

		$rs .='</div></div>';

		$res['result'] = $rs;
    }

    die($json->encode($res));
}

/*商品组合列表*/
function get_package_list(){
	$sql = "SELECT act_id,act_name FROM " . $GLOBALS['ecs']->table('goods_activity'). " WHERE `act_type` = ". GAT_PACKAGE ." ORDER BY act_id DESC";
	$res = $GLOBALS['db']->query($sql);

	$list = array();
	while ($row = $GLOBALS['db']->fetchRow($res))
	{
		$list[] = $row;

	}
	return $list;
}

/* 组合的商品列表*/
function get_package_goods_list($package_id)
{
    $sql = "SELECT pg.*, g.goods_name,g.goods_size,g.cat_id,g.goods_sn 
            FROM " . $GLOBALS['ecs']->table('package_goods') . " AS pg, " .$GLOBALS['ecs']->table('goods') . " AS g WHERE pg.goods_id = g.goods_id
            AND pg.package_id = '$package_id'";
    if ($package_id == 0)
    {
        $sql .= " AND pg.admin_id = '$_SESSION[admin_id]'";
    }
    $resource = $GLOBALS['db']->getAll($sql);
    if (!$resource)
    {
        return array();
    }

    $list = array();
	foreach($resource as $key=>$data){
		$list[$key]['package_id']	= $data['package_id'];
		$list[$key]['goods_id']		= $data['goods_id'];
		$list[$key]['cat_id']		= $data['cat_id'];
		$list[$key]['cat2_id']		= $data['cat2_id'];
		$list[$key]['goods_name']	= $data['goods_name'];
		$list[$key]['goods_size']	= $data['goods_size'];
		$list[$key]['cat_name']		= getCatName($data['cat_id']);//所属商品
		$list[$key]['goods_number']	= $data['goods_number'];
		$list[$key]['goods_price']	= $data['goods_price'];
		$list[$key]['shop_price']	= $data['shop_price'];
		$list[$key]['market_price']	= $data['market_price'];
		$list[$key]['goods_model']	= $data['goods_model'];
		$list[$key]['goods_model_name']	= getGoodsModel($data['goods_model']);

		$list[$key]['format_goods_price']	= price_format($data['goods_price']);
		$list[$key]['format_shop_price']	= price_format($data['shop_price']);
		$list[$key]['format_market_price']	= price_format($data['market_price']);

		$list[$key]['goods_sn']	= $data['goods_sn'];

	}
    return $list;
}

function get_price($package_id ,$goods_id){
	return $GLOBALS['db']->getOne('SELECT goods_price FROM ' . $GLOBALS['ecs']->table('package_goods') . " WHERE goods_id = '$goods_id' AND package_id = '$package_id'");
}


/* 获取套装稿件列表 */
function get_goods_aa_list( $act_type ){
    $sql = "SELECT act_id,act_name,act_type FROM " . $GLOBALS['ecs']->table('goods_aa') ." WHERE act_type ='$act_type' ORDER BY act_id DESC";
    $datalist =  $GLOBALS['db']->getAll($sql);

	$list = array();
	foreach($datalist as $key=>$row){
		$sql  = "SELECT g.goods_thumb FROM ". 
		$GLOBALS['ecs']->table('goods') . " as g, ".$GLOBALS['ecs']->table('goods_aa_data')." as gg WHERE g.goods_id = gg.goods_id AND is_delete = 0 AND type = 1 AND gg.package_id = '$row[act_id]'";
		$row['goods_thumb'] = $GLOBALS['db']->getOne($sql);

		$list[] = $row;
	}
	return $list;
}

function category_get_goods2_count($cat_id = 0,$cat2_id = 0)
{
		$where = ' AND is_delete = 0 AND type = 1';
		if($cat2_id>0){
			$where .=' AND cat2_id = '.$cat2_id;
		}
		$sql  = "SELECT count(*) FROM ". $GLOBALS['ecs']->table('goods') . ' WHERE cat_id = '.$cat_id.$where;
		return $GLOBALS['db']->getOne($sql);
}

/**
 * 获得分类下的稿件
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods2($cat_id = 0,$cat2_id = 0, $goods2_id = 0 , $size = 40 , $page = 1)
{
	$arr = array();
	$ids = '0';
	$where = "g.is_delete = 0 AND type = 1 AND g.cat_id = ".$cat_id;

	if($cat2_id>0){
		$where .=' AND g.cat2_id = '.$cat2_id;
	}
	
	//当前稿件
	/*
	if($goods2_id > 0 && $page == 1){
		$sql = "SELECT g.goods_id, g.cat_id, g.des_id, g.cat2_id ,g.act_id, g.type, g.goods_name, g.goods_thumb , g.goods_img ".
			" FROM ". $GLOBALS['ecs']->table('goods'). ' AS g'.
			" WHERE $where AND g.goods_id = $goods2_id";
		$row = $GLOBALS['db']->getRow($sql);
		if($row){
			$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
			$arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
			$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
			$arr[$row['goods_id']]['des_id']           = $row['des_id'];
			$arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
			$arr[$row['goods_id']]['type']			   = $row['type'];
			$arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
			$arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
			$arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
			$ids = $row['goods_id'];
		}
	}*/
	
	/*
	//我的收藏稿件 10个
    $sql = 'SELECT g.goods_id,g.cat_id,g.des_id,g.cat2_id ,g.act_id,g.type, g.goods_name, g.goods_thumb , g.goods_img ' .
            ' FROM ' . $GLOBALS['ecs']->table('collect_goods') . ' AS c' .
            " LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ".
                "ON g.goods_id = c.goods_id ".
            " WHERE $where AND c.user_id = '$_SESSION[user_id]' AND g.goods_id not in($ids) ORDER BY c.rec_id DESC";
    $collect_goods = $GLOBALS['db']->selectLimit($sql, 10 , 0 );
    while ($row = $GLOBALS['db']->fetchRow($collect_goods))
    {
		$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['des_id']           = $row['des_id'];
        $arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
		$arr[$row['goods_id']]['type']			   = $row['type'];
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
		if($ids == '0'){
			$ids .= $row['goods_id'];
		}else{
			$ids .= ','.$row['goods_id'];
		}
    }*/

	$count = count($arr);
	$size = $size - $count;

    /* 商家推荐稿件 10 */
    $sql = 'SELECT g.goods_id,g.cat_id,g.des_id,g.cat2_id ,g.act_id,g.type, g.goods_name, g.goods_thumb , g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            "WHERE $where AND g.goods_id not in($ids) ORDER BY g.is_best DESC, g.goods_id ASC";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
		$arr[$row['goods_id']]['cat_id']           = $row['cat_id'];
        $arr[$row['goods_id']]['des_id']           = $row['des_id'];
        $arr[$row['goods_id']]['cat2_id']		   = $row['cat2_id'];
		$arr[$row['goods_id']]['type']			   = $row['type'];
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = 'goods2.php?id='.$row['goods_id'];
    }

    return $arr;
}